var t = require("../../../../@babel/runtime/helpers/toConsumableArray"),
  e = u(require("../../../../@babel/runtime/regenerator")),
  a = require("../../../../@babel/runtime/helpers/asyncToGenerator"),
  n = u(require("../../../../config/index")),
  r = u(require("../../../../config/page")),
  o = require("../../../../utils/jump"),
  s = require("../../../../services/user-service"),
  i = require("../../../../services/base/api");
function u(t) {
  return t && t.__esModule ? t : { default: t };
}
var c = 0;
Page({
  data: {
    CDN: n.default.CDN,
    COMPLAINT_STATUS: { 1: "待回复", 2: "已处理" },
    objs: ["服务问题", "设施问题", "展览问题", "环境问题", "票务问题", "其它"],
    curObj: -1,
    content: "",
    pic: [],
    source: "",
    status: 0,
    phone: "",
    disabled: !1,
    background: "rgb(249,243,233)",
    showToast: !1,
    toastText: "提交成功，我们将尽快处理",
  },
  onLoad: function (t) {
    this.setData({ status: Number(t.status) }), this.init(t);
  },
  init: function (t) {
    var e = t.source;
    this.data.source = e;
  },
  clickObj: function (t) {
    this.setData({ curObj: t.currentTarget.dataset.index });
  },
  delPic: function (t) {
    var e = t.currentTarget.dataset.index,
      a = this.data.pic;
    a.splice(e, 1), this.setData({ pic: a });
  },
  getPhone: function (t) {
    this.setData({ phone: t.detail.value });
  },
  showToast: function (t) {
    var e = this,
      a = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 1500;
    this.setData({ showToast: !0, toastText: t }),
      setTimeout(function () {
        e.hideToast();
      }, a);
  },
  hideToast: function () {
    this.setData({ showToast: !1, toastText: "" });
  },
  create: function () {
    var t = this;
    return a(
      e.default.mark(function a() {
        var n, r, o, i, u;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                if (!((n = Date.now()) - c > 5e3)) {
                  e.next = 20;
                  break;
                }
                return (
                  t.setData({ disabled: !0 }),
                  (r = t.data),
                  (o = r.status),
                  (i = r.pic),
                  wx.showLoading({ title: "提交中", mask: !0 }),
                  (e.next = 7),
                  t.uploadFile(i)
                );
              case 7:
                return (u = ""), (e.next = 10), (0, s.getUserId)();
              case 10:
                if (((u = e.sent), 1 !== o)) {
                  e.next = 16;
                  break;
                }
                return (e.next = 14), t.addComplain(u);
              case 14:
                e.next = 19;
                break;
              case 16:
                if (2 !== o) {
                  e.next = 19;
                  break;
                }
                return (e.next = 19), t.addAdvice(u);
              case 19:
                t.setData({ disabled: !1 });
              case 20:
                c = n;
              case 21:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  addComplain: function (t) {
    var n = this;
    return a(
      e.default.mark(function a() {
        var s, u;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                if ("" !== n.data.content) {
                  e.next = 4;
                  break;
                }
                return (
                  wx.showToast({
                    title: "请输入投诉内容和类型!",
                    icon: "error",
                    duration: 1e3,
                    mask: !0,
                  }),
                  n.setData({ disabled: !1 }),
                  e.abrupt("return")
                );
              case 4:
                if (-1 !== n.data.curObj) {
                  e.next = 8;
                  break;
                }
                return (
                  wx.showToast({
                    title: "请输入投诉类型!",
                    icon: "error",
                    duration: 1e3,
                    mask: !0,
                  }),
                  n.setData({ disabled: !1 }),
                  e.abrupt("return")
                );
              case 8:
                if (!(n.data.pic.length > 3)) {
                  e.next = 12;
                  break;
                }
                return (
                  wx.showToast({
                    title: "上传图片数量超过上限",
                    icon: "none",
                    duration: 2e3,
                    mask: !0,
                  }),
                  n.setData({ disabled: !1 }),
                  e.abrupt("return")
                );
              case 12:
                return (
                  console.log(
                    n.data.curObj,
                    n.data.objs[n.data.curObj],
                    "tousu",
                  ),
                  (s = {
                    obj:
                      (n.data.curObj + 2) % 6 == 0
                        ? 6
                        : (n.data.curObj + 2) % 6,
                    content: n.data.content,
                    pic: n.data.pic,
                    open_id: t,
                    phone: n.data.phone,
                  }),
                  (e.next = 16),
                  (0, i.postCreateComplain)(s)
                );
              case 16:
                e.sent &&
                  (wx.hideLoading(),
                  (u = n.data.toastText),
                  n.showToast(u, 1500),
                  setTimeout(function () {
                    "list" === n.data.source
                      ? wx.navigateBack({ delta: 1 })
                      : (0, o.navigateTo)(
                          r.default.PAGE_COMPLAINT_LIST,
                          {},
                          !0,
                        );
                  }, 1e3));
              case 18:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  addAdvice: function (t) {
    var n = this;
    return a(
      e.default.mark(function a() {
        var s, u;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                if ("" !== n.data.content) {
                  e.next = 4;
                  break;
                }
                return (
                  wx.showToast({
                    title: "请输入建议内容!",
                    icon: "error",
                    duration: 1e3,
                    mask: !0,
                  }),
                  n.setData({ disabled: !1 }),
                  e.abrupt("return")
                );
              case 4:
                return (
                  (s = {
                    content: n.data.content,
                    pic: n.data.pic,
                    open_id: t,
                    phone: n.data.phone,
                  }),
                  (e.next = 7),
                  (0, i.postCreateAdvice)(s)
                );
              case 7:
                e.sent &&
                  (wx.hideLoading(),
                  (u = n.data.toastText),
                  n.showToast(u, 1500),
                  setTimeout(function () {
                    "list" === n.data.source
                      ? wx.navigateBack({ delta: 1 })
                      : (0, o.navigateTo)(
                          r.default.PAGE_COMPLAINT_LIST,
                          {},
                          !0,
                        );
                  }, 1e3));
              case 9:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  clickUpload: function () {
    var t = this;
    return a(
      e.default.mark(function n() {
        var r;
        return e.default.wrap(function (n) {
          for (;;)
            switch ((n.prev = n.next)) {
              case 0:
                (r = t.data.pic.length) >= 3
                  ? wx.showToast({
                      title: "上传图片数量已达上限",
                      icon: "none",
                      duration: 2e3,
                      mask: !0,
                    })
                  : wx.chooseMedia({
                      sizeType: ["original"],
                      success: (function () {
                        var n = a(
                          e.default.mark(function a(n) {
                            var o, s;
                            return e.default.wrap(function (e) {
                              for (;;)
                                switch ((e.prev = e.next)) {
                                  case 0:
                                    if (
                                      ((o = t.data.pic),
                                      !((s = n.tempFiles).length + r > 3))
                                    ) {
                                      e.next = 5;
                                      break;
                                    }
                                    return (
                                      wx.showToast({
                                        title: "上传图片数量超过上限",
                                        icon: "none",
                                        duration: 2e3,
                                        mask: !0,
                                      }),
                                      e.abrupt("return")
                                    );
                                  case 5:
                                    s.forEach(function (e) {
                                      var a = e.tempFilePath,
                                        n = a
                                          ? a.split(".").pop().toLowerCase()
                                          : "",
                                        r = e.size;
                                      console.log(r),
                                        a
                                          ? r > 10485760
                                            ? wx.showToast({
                                                title: "上传的图片过大",
                                                icon: "none",
                                                duration: 2500,
                                              })
                                            : "jpg" === n || "png" === n
                                              ? (o.push(a),
                                                t.setData({ pic: o }))
                                              : wx.showToast({
                                                  title:
                                                    "应上传png或者jpg格式的图片",
                                                  icon: "none",
                                                  duration: 2500,
                                                })
                                          : wx.showToast({
                                              title: "获取图片失败",
                                              icon: "none",
                                              duration: 2500,
                                            });
                                    });
                                  case 6:
                                  case "end":
                                    return e.stop();
                                }
                            }, a);
                          }),
                        );
                        return function (t) {
                          return n.apply(this, arguments);
                        };
                      })(),
                    });
              case 2:
              case "end":
                return n.stop();
            }
        }, n);
      }),
    )();
  },
  uploadFile: function (n) {
    var r = this;
    return a(
      e.default.mark(function a() {
        var o;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                if ((console.log(n), !(n.length > 3))) {
                  e.next = 4;
                  break;
                }
                return (
                  wx.showToast({
                    title: "选择图片数量已达上限",
                    icon: "none",
                    duration: 2e3,
                    mask: !0,
                  }),
                  e.abrupt("return")
                );
              case 4:
                return (
                  (e.next = 6),
                  Promise.all(
                    n.map(function (t) {
                      return console.log(t), (0, s.upload)(t);
                    }),
                  )
                    .then(function (t) {
                      o = t;
                    })
                    .catch(function (t) {
                      console.log(t, "出错了");
                    })
                );
              case 6:
                console.log("图片上传后的地址列表：", o),
                  r.setData({ pic: t(o) });
              case 8:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  setVal: function (t) {
    this.setData({ content: t.detail.value.substring(0, 400) });
  },
});
